import geoip2.database
import requests, re


def Iplocation(ip):
    response = geoip2.database.Reader("GeoLite2-City.mmdb").city(ip)
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36',
        'Hosts': 'www.gpsspg.com',
        'Referer': 'http://www.gpsspg.com/iframe/maps/qq_161128.htm?mapi=2',
    }
    url = "http://www.gpsspg.com/apis/maps/geo/?output=jsonp&lat=%s000000&lng=%s000000&type=0&callback=jQuery110207785323396673127_1522316918197&_=1522316918198" % (
    response.location.latitude, response.location.longitude)
    try:
        return re.findall("address\"\:\"(.+?)\"", requests.get(url, headers=headers).text)[0]
    except Exception as e:
        return e


if __name__ == '__main__':
    ip = "139.227.248.190"
    print(Iplocation(ip))
